package com.heima.article.listen;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.service.ApArticleConfigService;
import com.heima.common.constants.message.NewsUpOrDownConstants;
import com.heima.model.article.pojos.ApArticleConfig;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @package com.heima.article.listen
 * @Date 2022/3/5
 * @project heima-leadnews
 */
@Component
@Slf4j
public class AtticleUpOrDownListener {
    @Autowired
    private ApArticleConfigService apArticleConfigService;

    @RabbitListener(queuesToDeclare = {@Queue(
            value = NewsUpOrDownConstants.NEWS_UP_FOR_ARTICLE_CONFIG_QUEUE
    )})
    public void newsUpHander(String articleId){
        log.info("接受到文章上架消息 {}",articleId);
        try {
            apArticleConfigService.update(Wrappers.<ApArticleConfig>lambdaUpdate()
                    .set(ApArticleConfig::getIsDown,false)
            .eq(ApArticleConfig::getArticleId,Long.valueOf(articleId)));
        } catch (NumberFormatException e) {
            e.printStackTrace();
            log.error("自媒体文章上架消息处理失败 原因{}",e.getMessage());
        }
    }
    @RabbitListener(queuesToDeclare = {@Queue(
            value = NewsUpOrDownConstants.NEWS_DOWN_FOR_ARTICLE_CONFIG_QUEUE
    )})
    public void newsDownHander(String articleId){
        log.info("接受到文章下架消息 {}",articleId);
        try {
            apArticleConfigService.update(Wrappers.<ApArticleConfig>lambdaUpdate()
                    .set(ApArticleConfig::getIsDown,true)
                    .eq(ApArticleConfig::getArticleId,Long.valueOf(articleId)));
        } catch (NumberFormatException e) {
            e.printStackTrace();
            log.error("自媒体文章下架消息处理失败 原因{}",e.getMessage());
        }
    }

}
